package com.das.mechanic_base.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.das.mechanic_base.utils.X3ScreenUtils;

public class X3TainNewTimeView extends View {

    private Context mContext;

    private Paint mPaint;

    private int startX;

    private String angelColor = "#20FFFFFF";

    /**
     * 状态
     * 0 评分低
     * 1 评分 > 60
     */
    private int status;

    /**
     * 需要旋转的角度
     */
    private int angle = 0;

    private ValueAnimator valueAnimator;

    public X3TainNewTimeView(Context context) {
        this(context,null);
    }

    public X3TainNewTimeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public X3TainNewTimeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        startX = X3ScreenUtils.dipToPx(10, mContext);
        // 绘制底色圆弧
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(startX);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        if (status == 0) {
            mPaint.setColor(Color.parseColor("#20F34343"));
        }else {
            mPaint.setColor(Color.parseColor("#2018be6b"));
        }
        // 先行定义圆的范围
        RectF oval = new RectF(startX, startX,getWidth() - startX, getHeight() - startX);

        int cuteAngel = angle + 7;
        int endAngel = (240 - cuteAngel) <= 0 ? 0 : (240 - angle - 7);
        int startAngel = (-210 + cuteAngel) >= 30 ? 30 : (-210 + cuteAngel);
        canvas.drawArc(oval, startAngel,endAngel,false,mPaint);

        // 在画下进度
        if (status == 0) {
            mPaint.setColor(Color.parseColor("#F34343"));
        }else {
            mPaint.setColor(Color.parseColor("#18be6b"));
        }
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(startX);
        RectF oval1 = new RectF(startX, startX,getWidth() - startX, getHeight() - startX);
        canvas.drawArc(oval1,-210,(angle - 7) <= 0 ? 0 : (angle - 7),false,mPaint);

        // 绘制三个进度圆
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.WHITE);

        int radius = (getWidth() - startX * 2) / 2;
        int centerX = getWidth() /2;
        float radian = (float) Math.toRadians(-210 + angle);
        float pointX = (float) (centerX + radius * Math.cos(radian));
        float pointY = (float) (centerX + radius * Math.sin(radian));
        canvas.drawCircle(pointX,pointY, X3ScreenUtils.dipToPx(10,mContext),mPaint);

        if (status == 0) {
            mPaint.setColor(Color.parseColor("#F34343"));
        }else {
            mPaint.setColor(Color.parseColor("#18be6b"));
        }

        canvas.drawCircle(pointX,pointY, X3ScreenUtils.dipToPx(5,mContext),mPaint);
    }

    /**
     * 根据施工单状态改变view的显示颜色
     * @param status
     */
    public void changeStatus(int status) {
        this.status = status;
        invalidate();
    }

    public void changeOneAngle(int precent) {
        if (valueAnimator != null && valueAnimator.isRunning()) {
            return;
        }
        angle = (int) (precent * 240 * 0.01);
        invalidate();
    }

    public void changeAngle(int precent) {
        if (precent < 60) {
            status = 0;
        }else {
            status = 1;
        }
        // 获取到百分比之后 需要兑换成相应的角度
        valueAnimator = ValueAnimator.ofInt(0,precent);
        valueAnimator.setDuration(1000);
        valueAnimator.setStartDelay(1000);
        valueAnimator.addUpdateListener(valueAnimator -> {
            int animatedValue = (int) valueAnimator.getAnimatedValue();
            angle = (int) (animatedValue * 240 * 0.01);
            invalidate();
        });
        valueAnimator.start();

    }
}
